package myself.a005Heap;

import java.util.PriorityQueue;

/**
 * @author 叶之越
 * Description
 * Date 2021/1/23
 * Time 23:04
 * Mail 739153436@qq.com
 */
public class SortArrayDistanceLessK {
    public static void main(String[] args) {
        PriorityQueue<Integer> heap = new PriorityQueue<>();
        heap.offer(2);
        heap.offer(1);
        heap.offer(3);
        System.out.println(heap.poll());
    }

    public void sort(int[] arr, int k) {
        if (k == 0) {
            return;
        }
        PriorityQueue<Integer> heap = new PriorityQueue<>();
        int index = 0;
        // 边界的处理，根据题意移动的步数是 <= k 的，那么我们的堆当中就需要放置 k+1 个元素
        k++;
        for (int i = 0; i < Math.min(arr.length, k); i++) {
            heap.offer(arr[i]);
        }

        for (int i = 0; i < arr.length; i++) {
            arr[i] = heap.poll();
            if (i + k < arr.length) {
                heap.offer(arr[i + k]);
            }
        }
    }
}

